package com.fuint.application.web.rest;

import com.fuint.application.dao.entities.MtUser;
import com.fuint.application.service.member.MemberService;
import com.fuint.exception.BusinessCheckException;
import com.fuint.application.ResponseObject;
import com.fuint.application.BaseController;
import com.fuint.application.service.token.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 收银台controller
 * Created by FSQ
 * Contact wx fsq_better
 * Site https://www.fuint.cn
 */
@RestController
@RequestMapping(value = "/rest/cashier")
public class CashierController extends BaseController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private MemberService memberService;

    /**
     * 登录收银台
     */
    @RequestMapping(value = "/doLoginIn", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject dologinIn(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
        String token = request.getHeader("Access-Token");
        String mobile = param.get("mobile") == null ? "" : param.get("mobile").toString();
        String username = param.get("username") == null ? "" : param.get("username").toString();

        if (StringUtils.isEmpty(mobile)) {
            mobile = username;
        }

        MtUser mtUser;
        if (!StringUtils.isEmpty(token)) {
            mtUser = tokenService.getUserInfoByToken(token);
        } else if(!StringUtils.isEmpty(mobile)) {
            mtUser = memberService.queryMemberByMobile(mobile);
        } else {
            return getFailureResult(1001);
        }

        // 会员不存在就注册
        if (mtUser == null && !StringUtils.isEmpty(mobile)) {
            memberService.addMemberByMobile(mobile);
            mtUser = memberService.queryMemberByMobile(mobile);
        }

        String userAgent = request.getHeader("User-Agent");
        String accessToken = tokenService.generateToken(userAgent, mtUser.getId().toString());
        tokenService.saveToken(accessToken, mtUser);

        Map<String, Object> userInfo = new HashMap();
        userInfo.put("token", accessToken);
        userInfo.put("id", mtUser.getId());
        userInfo.put("mobile", mtUser.getMobile());
        userInfo.put("username", mtUser.getName());

        Map<String, Object> outParams = new HashMap<>();
        outParams.put("userInfo", userInfo);

        return getSuccessResult("登录成功", outParams);
    }

    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject logout(HttpServletRequest request) {
        String userToken = request.getHeader("Access-Token");
        Boolean flag = tokenService.removeToken(userToken);
        if (Boolean.FALSE == flag) {
            return getFailureResult(1001, "退出错误!");
        } else {
            return getSuccessResult("退出成功！");
        }
    }
}
